package com.showgood.core {
	import com.showgood.controller.ErrorCommand;
	import com.showgood.controller.LoginCommand;
	import com.showgood.controller.ReplyArmyCommand;
	import com.showgood.controller.ReplyArmyDisbandCommand;
	import com.showgood.controller.ReplyArmyListCommand;
	import com.showgood.controller.ReplyCityDetailCommand;
	import com.showgood.controller.ReplyManDetailCommand;
	import com.showgood.controller.ReplyManListCommand;
	import com.showgood.controller.ReplyCityListCommand;
	import com.showgood.controller.StartupCommand;
	import com.showgood.config.SangoConst;
	import com.showgood.model.PopupStackProxy;
	import com.showgood.model.SocketProxy;
	import com.showgood.vo.RemoteMessage;
	import org.puremvc.as3.interfaces.IMediator;
	import org.puremvc.as3.interfaces.IProxy;
	import org.puremvc.as3.patterns.facade.Facade;
	import org.puremvc.as3.patterns.mediator.Mediator;
	import org.puremvc.as3.patterns.proxy.Proxy;
	import flash.utils.getQualifiedClassName;
	
	/**
	 * the global unique facade class
	 * @author bianpeng
	 */
	public final class SangoFacade extends Facade {
		public function SangoFacade() {
			super();
		}
		private static var _instance:SangoFacade;
		public static function get instance():SangoFacade {
			if (!_instance) {
				_instance = new SangoFacade();
			}
			return _instance;
		}
		
		override protected function initializeController():void {
			super.initializeController();
			
			///
			registerCommand(SangoConst.STARTUP, StartupCommand);
			///
			registerRemoteCommand(RemoteMessage.REMOTE_LOGIN, LoginCommand);
			registerRemoteCommand(RemoteMessage.REMOTE_ERROR, ErrorCommand);
			registerRemoteCommand(RemoteMessage.REMOTE_QUERY_CITY_ON_MAP, ReplyCityListCommand);
			registerRemoteCommand(RemoteMessage.REMOTE_QUERY_ARMY_ON_MAP, ReplyArmyListCommand);
			registerRemoteCommand(RemoteMessage.REMOTE_UPDATE_ARMY_MOVE, ReplyArmyCommand);
			registerRemoteCommand(RemoteMessage.REMOTE_UPDATE_ARMY_DISBAND, ReplyArmyDisbandCommand);
			registerRemoteCommand(RemoteMessage.REMOTE_UPDATE_ARMY_ARRIVED, ReplyArmyDisbandCommand);
			registerRemoteCommand(RemoteMessage.REMOTE_QUERY_ARMY_SETUP, ReplyArmyListCommand);
			registerRemoteCommand(RemoteMessage.REMOTE_QUERY_CHUZHENG_MAN_LIST, ReplyManListCommand);
			registerRemoteCommand(RemoteMessage.REMOTE_QUERY_CHUZHENG_MAN_DETAIL, ReplyManDetailCommand);
			registerRemoteCommand(RemoteMessage.REMOTE_QUERY_CITY_DETAIL, ReplyCityDetailCommand);
		}
		// 注册一个命令, 这个比较简单, 因为每次都是new一个出来的, 就不必提供删除操作了
		// 主要是这里初始化的时候用的
		private function registerRemoteCommand(msg:int, clazz:Class):void {
			registerCommand(msg+'_REMOTE_MESSAGE', clazz);
		}
		// 注册一个mediator
		public function registerMediatorByType(mediator:AbstractMediator):void {
			registerMediator(mediator);
		}
		public function retrieveMediatorByType(clazz:Class):IMediator {
			return retrieveMediator(getQualifiedClassName(clazz));
		}
		// 删除mediator
		public function removeMediatorByType(clazz:Class):void {
			super.removeMediator(getQualifiedClassName(clazz));
		}
		// 注册一个proxy
		public function registerProxyByType(proxy:AbstractProxy):void {
			registerProxy(proxy);
		}
		// 得到一个proxy
		public function retrieveProxyByType(clazz:Class):IProxy {
			return retrieveProxy(getQualifiedClassName(clazz));
		}
		// 删除一个proxy
		public function removeProxyByType(clazz:Class):void {
			super.removeProxy(getQualifiedClassName(clazz));
		}
		
		/////////////////////
		// 用来辅助简化代码用的东西, 没办法了, 真恶心呢
		/////////////////////
		public function get socket():SocketProxy {
			return this.retrieveProxyByType(SocketProxy) as SocketProxy;
		}
		public function get popups():PopupStackProxy {
			return this.retrieveProxyByType(PopupStackProxy) as PopupStackProxy;
		}
	}
}

